﻿using System;
using System.Collections.Generic;

[Serializable]
/// <summary>
///ShoppingCart 的摘要说明
/// </summary>
public class ShoppingCart
{
    public ShoppingCart()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    private List<CartItem> items = new List<CartItem>();

    public List<CartItem> Items
    {
        get { return items; }
        set { items = value; }
    }

    public bool RemoveItem(string PrdtId)
    {
        for (int i = 0; i < Items.Count; i++)
        {
            if (Items[i].ItemId == PrdtId)
            {
                items.RemoveAt(i);
                return true;
            }
        }
        return false;
    }

    public bool IsDuplicate(string PrdtId)
    {
        for (int i = 0; i < Items.Count; i++)
        {
            if (Items[i].ItemId == PrdtId)
            {
                return true;
            }
        }
        return false;
    }

    public bool AddToCart(CartItem ci)
    {
        if (!IsDuplicate(ci.ItemId))
        {
            items.Add(ci);
            return true;
        }
        return false;
    }

    public bool UpdateCart(string itemId, int itemCount)
    {
        for (int i = 0; i < Items.Count; i++)
        {
            if (Items[i].ItemId == itemId)
            {
                Items[i].Count = itemCount;
                return true;
            }
        }
        return false;
    }

    public decimal[] GetTotal()
    {
        decimal[] total = new decimal[2] { 0, 0 };
        for (int i = 0; i < Items.Count; i++)
        {
            total[0] += Items[i].ItemPrice;
            total[1] += Items[i].Count;
        }
        return total;
    }
}

[Serializable]
public class CartItem
{
    public CartItem()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public CartItem(string itemId, string itemName)
    {
        this.ItemId = itemId;
        this.ItemName = itemName;
    }

    public CartItem(string itemId, string itemName, decimal itemPrice, int count)
    {
        this.ItemId = itemId;
        this.ItemName = itemName;
        this.ItemPrice = itemPrice;
        this.Count = count;
    }

    private string itemId;

    public string ItemId
    {
        get { return itemId; }
        set { itemId = value; }
    }

    private string itemName;

    public string ItemName
    {
        get { return itemName; }
        set { itemName = value; }
    }
    private decimal itemPrice;

    public decimal ItemPrice
    {
        get { return itemPrice; }
        set { itemPrice = value; }
    }
    private int count;

    public int Count
    {
        get { return count; }
        set { count = value; }
    }
}